#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QDomElement>
#include<QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    strcurrentfilepath="D:/job/qt/work/me/day4";
    strcurrentfilename="/myxml1.xml";

}

Widget::~Widget()
{
    delete ui;
}


bool Widget::openxmlfiles(QString filenamepath)
{
    m_qfiles.setFileName(strcurrentfilepath+filenamepath);
    return m_qfiles.open(QIODevice::ReadWrite|QIODevice::Text);

}

void Widget::writexmlfiles()
{
    if(!openxmlfiles(strcurrentfilename))
    {
        qDebug()<<"写入：打开文件失败，请重新检查";
        return;
    }
    qDebug()<<"写入：打开XML文件成功，请认真操作xml！";
    QDomDocument doc;

    QDomProcessingInstruction ins=doc.createProcessingInstruction("xml","version = \"1.0\" encoding = \"GB2312\"");
    doc.appendChild(ins);

    //添加跟元素
    QDomElement factory=doc.createElement("factory");
    doc.appendChild(factory);

    QDomElement worker=doc.createElement("worker");


    {
        QDomElement WNo=doc.createElement("WNo");

        QDomText text1= doc.createTextNode("WNo");
        text1.setData(ui->lineEdit_No_R->text());
        WNo.appendChild(text1);
        worker.appendChild(WNo);

        //2
        QDomElement WName=doc.createElement("WName");

        QDomText text2= doc.createTextNode("WName");
        text2.setData(ui->lineEdit_Name_R->text());
        WName.appendChild(text2);
        worker.appendChild(WName);

        //3
        QDomElement WSex=doc.createElement("WSex");
        QDomText text3= doc.createTextNode("WSex");
        text3.setData(ui->lineEdit_Sex_R->text());
        WSex.appendChild(text3);
        worker.appendChild(WSex);

        //4
        QDomElement WEducation=doc.createElement("WEducation");
        QDomText text4= doc.createTextNode("WEducation");
        text4.setData(ui->lineEdit_Education_R->text());
        WEducation.appendChild(text4);
        worker.appendChild(WEducation);



        //5
        QDomElement WDepartment=doc.createElement("WDepartment");
        QDomText text5= doc.createTextNode("WDepartment");
        text5.setData(ui->lineEdit_Department_R->text());
        WDepartment.appendChild(text5);
        worker.appendChild(WDepartment);

        //6
        QDomElement WSalary=doc.createElement("WSalary");
        QDomText text6= doc.createTextNode("WSalary");
        text6.setData(ui->lineEdit_Salary_R->text());
        WSalary.appendChild(text6);
        worker.appendChild(WSalary);
    }

    factory.appendChild(worker);
    m_qfiles.write(doc.toString().toLocal8Bit().data());
    m_qfiles.close();
    QMessageBox::information(this,"提示","恭喜你，写入XML文件数据成功，请查看目录文件?",QMessageBox::Yes);

}

void Widget::readxmlfiles()
{
  if(!openxmlfiles(strcurrentfilename))
  {
      qDebug()<<"文件打开失败，请重新检查";
      return;
  }

  qDebug()<<"读取:打开文件文件称成功，请认真操作xml！";
  QDomDocument doc;
  //调用setContent函数设置数据源
  if(!doc.setContent(&m_qfiles))
  {
      m_qfiles.close();
      qDebug()<<"设置数据源失败"<< m_qfiles.errorString();;

      return;
  }
  qDebug()<<"读取:操作setContent成功,请认真操作!";

  //m_qfiles.close();

  //获取xDoc中的QDomelent根对象
  QDomElement docElem=doc.documentElement();

  //获取docElem的根节点
  QDomNode node=docElem.firstChild();

  while(!node.isNull())
  {
      if(node.isElement())
      {
         QDomNodeList nodelist=node.childNodes();
         QString rootname=node.toElement().tagName();
         if(rootname.compare("worker")==0)
         {
             //将子节点的集合信息传递到读取子节点数据
             readrootxml(nodelist);
         }
         node=node.nextSibling();//读取下一个节点
      }
  }


}

void Widget::readrootxml(QDomNodeList sonnodelist)
{
   for(int i=0;i<sonnodelist.size();i++)
   {
      QDomElement sonelement=sonnodelist.at(i).toElement();
       if(sonelement.tagName().compare("WNo")==0)
       {
           ui->lineEdit_No->setText(sonelement.text());
       }
       else if(sonelement.tagName().compare("WName")==0)
       {
           ui->lineEdit_Name->setText(sonelement.text());
       }
       else if(sonelement.tagName().compare("WSex")==0)
       {
           ui->lineEdit_Sex->setText(sonelement.text());
       }
       else if(sonelement.tagName().compare("WEducation")==0)
       {
           ui->lineEdit_Education->setText(sonelement.text());
       }
       else if(sonelement.tagName().compare("WDepartment")==0)
       {
           ui->lineEdit_Department->setText(sonelement.text());
       }
       else if(sonelement.tagName().compare("WSalary")==0)
       {
           ui->lineEdit_Salary->setText(sonelement.text());
       }
   }
}

//写XML
void Widget::on_pushButton_WriteXml_clicked()
{
    writexmlfiles();
}

//读XML
void Widget::on_pushButton_ReadXml_clicked()
{
    readxmlfiles();
}
